package com.example.wanjialei.service.serviceimpl;

import com.example.wanjialei.mapper.AdvisoryMapper;
import com.example.wanjialei.mapper.NoticeMapper;
import com.example.wanjialei.pojo.Advisory;
import com.example.wanjialei.pojo.Notice;
import com.example.wanjialei.pojo.NoticeExample;
import com.example.wanjialei.service.NoticeService;
import com.github.pagehelper.PageHelper;
import org.springframework.stereotype.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Service
public class NoticeImpl implements NoticeService {
    @Resource
    private NoticeMapper noticeMapper;

    @Override
    public Map<String, Object> selectAll(Notice notice) {
        Map<String,Object> map = new HashMap<>();
        NoticeExample noticeExample = new NoticeExample();
        List<Notice> rows = new ArrayList<>();
        if (notice.getType() != null) {

            noticeExample.or().andTypeEqualTo(notice.getType());
            rows = noticeMapper.selectByExample(noticeExample);
        }else{
            rows = noticeMapper.selectByExample(null);
        }

        PageHelper.startPage(notice.getPage(), notice.getRows());
        PageInfo<Notice> pageInfo = new PageInfo<>(rows);
        if(rows.size()>0){
            map.put("flag",true);
            map.put("message","查询成功！");
            map.put("total",pageInfo.getTotal());
            map.put("rows",pageInfo.getList());
        }else{
            map.put("flag",false);
            map.put("message","查询失败！");
            map.put("total",rows.size());
            map.put("rows",rows);
        }
        return map;
    }


    @Override
    public Map<String, Object> modifyById(Notice notice) {
        Map<String,Object> map = new HashMap<>();
        int count = noticeMapper.updateByPrimaryKeySelective(notice);
        if(count>0){
            map.put("flag",true);
            map.put("message","修改成功！");
            map.put("total",count);
        }else{
            map.put("flag",false);
            map.put("message","修改失败！");
            map.put("total",count);
        }
        return map;
    }

    @Override
    public Map<String, Object> insert(Notice notice) {
        Map<String,Object> map = new HashMap<>();
        int count = noticeMapper.insert(notice);
        if(count>0){
            map.put("flag",true);
            map.put("message","插入成功！");
            map.put("total",count);
        }else{
            map.put("flag",false);
            map.put("message","插入失败！");
            map.put("total",count);
        }
        return map;
    }

    @Override
    public Map<String, Object> deleteById(Notice notice) {
        Map<String,Object> map = new HashMap<>();
        int count = noticeMapper.deleteByPrimaryKey(notice.getId());
        if(count>0){
            map.put("flag",true);
            map.put("message","删除成功！");
            map.put("total",count);
        }else{
            map.put("flag",false);
            map.put("message","删除失败！");
            map.put("total",count);
        }
        return map;
    }
}
